{% extends "king_admin/table_index.html" %}
{% load tags %}
{% block container %}
    <div class="row">
        <div class="panel panel-info">
          <div class="panel-heading">
              <h3 class="panel-title">
                  {% get_model_name admin_class %}
                  {% if not admin_class.readonly_table %}
                  <a href="{{ request.path }}add/" class="pull-right">Add</a>
                  {% endif %}
              </h3>
          </div>
          <div class="panel-body">
              <div class="row">
                  <form class="" method="get">
                      {% for filter_field in admin_class.list_filters %}
                        <div class="col-lg-2 col-md-2">
                            <span>{{ filter_field }}</span>
                            {% render_filter_ele filter_field admin_class filter_condtions %}
                        </div>
                      {% endfor %}
                        <button type="SUBMIT" class="btn btn-info">检索</button>
                        <hr>
                        <div class="col-lg-3 col-md-3">
                            <input type="search" name="_q" class="form-control" value="{{ search_text }}"
                                   placeholder="search.{% for search_field in admin_class.search_fields %}{{ search_field }}.{% endfor %}">
                        </div>
                        <button type="SUBMIT" class="btn btn-info col-lg-1 col-md-1">Search</button>
                  </form>
              </div>
              <div class="row" style="margin-top: 10px;">
                  <form onsubmit="return ActionSubmin(this)" method="POST">
                  {% csrf_token %}
                      <div class="col-lg-2 col-md-2">
                          <select id="action_list" name="action" class="form-control">
                              <option value="">-----</option>
                              {% for action in admin_class.actions %}
                                  <option value="{{ action }}">{% get_action_verbose_name admin_class action %}</option>
                              {% endfor %}
                          </select>
                      </div>
                      <button type="submit" class="btn btn-info" onclick="ActionSubmin()">Go</button>
                  </form>
              </div>
              <table class="table table-hover">
                  <thead>
                    <tr>
                        <th style="width: 30px" ><input type="checkbox" onclick="CheckAllToggle(this)"></th>
                        {% for column in admin_class.list_display %}
                            {% build_table_header_column column orderby_key filter_condtions admin_class %}
                        {% endfor %}
                    </tr>
                  </thead>
                  <tfoot>
                      <tr>
                          <td></td>
                          <td><h5>总计{{ query_sets.paginator.count }}条</h5></td>
                      </tr>
                  </tfoot>
                  <tbody>
{#                    {% get_query_sets admin_class as query_sets%} <!--给他admin_class的返回值起一个别名query_set -->#}
                        {% for obj in query_sets %}  <!-- obj是一个对象 -->
                            <tr>
                                <td><input tag="obj_checkbox" type="checkbox" value="{{ obj.id }}"></td>
                                {% build_table_row request obj admin_class %}
                                {# <td>{{ obj }}</td> #}
                            </tr>
                        {% endfor %}
                  </tbody>
              </table>
              <nav>
                  <ul class="pagination">
                      {% if query_sets.has_previous %}
                          <li class=""><a href="?page={{ query_sets.previous_page_number }}">上页</a></li>
                      {% endif %}
                      {% build_paginators query_sets filter_condtions previous_orderby %}
                      {% if query_sets.has_next %}
                          <li class=""><a href="?page={{ query_sets.next_page_number }}">下页</a></li>
                      {% endif %}
                  </ul>
              </nav>
          </div>
        </div>
    </div>
    <script>
        function CheckAllToggle(ele){
            {# console.log($(ele).prop("checked")); #}
            {# console.log($("input[tag='checkbox']")); #}
            if ($(ele).prop("checked")){
                $("input[tag='obj_checkbox']").prop("checked",true);
            }else{
                $("input[tag='obj_checkbox']").prop("checked",false);
            }
        }
        function ActionSubmin(form_ele){
            var selected_ids = [];
            $("input[tag='obj_checkbox']:checked").each(function(){
                selected_ids.push($(this).val())
            });
            var selected_action = $("#action_list").val();
            if(selected_ids.length == 0){
                alert("No object got selected!");
                return
            }
            if(!selected_action){
                alert("No action got selected!");
            }
            var selected_ids_ele = "<input name='selected_ids' type='hidden' value='"+selected_ids.toString()+"'>"
            $(form_ele).append(selected_ids_ele);
            return true;
        }
    </script>
{% endblock %}













